package com.dataseek.iot.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataseek.iot.device.dto.LiquidJarDto;
import com.dataseek.iot.device.entity.LiquidJar;
import com.dataseek.iot.device.mapper.LiquidJarMapper;
import com.dataseek.iot.device.service.LiquidJarService;
import com.dataseek.iot.device.vo.LiquidJarShowVo;
import com.dataseek.iot.device.vo.LiquidJarVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolkit.mybatis.entity.WorkLine1;
import com.dataseek.iot.toolkit.mybatis.mapper.WorkLineMapper1;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Slf4j
public class LiquidJarServiceImpl extends ServiceImpl<LiquidJarMapper, LiquidJar> implements LiquidJarService {


    @Autowired
    private LiquidJarMapper liquidJarMapper;

    @Autowired
    private WorkLineMapper1 workLineMapper;

    @Override
    public LiquidJarVo queryAction(String jarName, String lineCode, Integer num, Integer size) {

        LiquidJarVo lineVo = new LiquidJarVo();
        QueryWrapper<LiquidJar> qw = new QueryWrapper<>();
        if (jarName != null && !jarName.isEmpty()) {
            qw.like("jar_name", jarName);
        }
        if (lineCode != null && !lineCode.isEmpty()) {
            qw.eq("line_code", lineCode);
        }
        qw.orderByDesc("update_time");
        IPage<LiquidJar> page = new Page<>(num, size);
        List<LiquidJar> mapList = liquidJarMapper.selectPage(page, qw).getRecords();
        lineVo.setPage(num);
        lineVo.setSize(size);
        lineVo.setTotal(page.getTotal());
        lineVo.setList(mapList);
        return lineVo;
    }

    @Override
    public LiquidJar qryJarByCode(String jarCode) {
        QueryWrapper<LiquidJar> qw = new QueryWrapper<>();
        qw.eq("jar_code", jarCode);
        return this.getOne(qw);
    }

    @Override
    public LiquidJar qryByDeviceCode(String deviceCode) {
        LiquidJar liquidJar=new LiquidJar();
        QueryWrapper<LiquidJar> qw = new QueryWrapper<>();
        qw.eq("device_code", deviceCode);
        List<LiquidJar> liquidJarList= liquidJarMapper.selectList(qw);
        if(liquidJarList!=null&&liquidJarList.size()>0){
            liquidJar=liquidJarList.get(0);
        }
        return liquidJar;
    }

    @Override
    public LiquidJarDto qryJarByDeviceCode(String deviceCode) {
        LiquidJarDto qryDto = new LiquidJarDto();
        qryDto.setDeviceCode(deviceCode);
        Page<LiquidJarDto> page = new Page<>(1, 1);
        List<LiquidJarDto> reList = liquidJarMapper.queryJarShowList(page, qryDto);
        if (CollectionUtils.isEmpty(reList)) {
            return  null;
        }
        else {
            return reList.get(0);
        }
    }

    @Override
    public Integer insertAction(LiquidJar liquidJar) {
        int resultCode = 0;
        QueryWrapper<LiquidJar> qw = new QueryWrapper<>();
        qw.eq("jar_code", liquidJar.getJarCode()).or().eq("jar_name", liquidJar.getJarName());
        Integer count = liquidJarMapper.selectCount(qw);
        if (count == 0) {
            if (liquidJar.getCreateBy() == null) {
                liquidJar.setCreateBy("");
                liquidJar.setUpdateBy("");
            }

            //根据产线查询负责人
            if (liquidJar.getLineCode() != null && !liquidJar.getLineCode().isEmpty()) {
                QueryWrapper<WorkLine1> qwLine = new QueryWrapper<>();
                qwLine.eq("line_code", liquidJar.getLineCode());
                WorkLine1 lineDto = workLineMapper.selectOne(qwLine);
                if (lineDto != null) {
                    liquidJar.setJarLeader(lineDto.getLineLeader());
                }
            }

            this.saveOrUpdate(liquidJar);
        } else {
            resultCode = RspStatusEnum.FAIL.getCode();
        }
        return resultCode;

    }

    @Override
    public Integer updateAction(LiquidJar liquidJar) {
        int resultCode = liquidJarMapper.updateById(liquidJar);
        return resultCode;
    }

    @Override
    public Integer deleteAction(List<Integer> ids) {
        int resultCode = liquidJarMapper.deleteBatchIds(ids);
        return resultCode;
    }

    @Override
    public LiquidJarShowVo queryJarShowList(LiquidJarDto qryDto, Integer num, Integer size) {

        LiquidJarShowVo lineVo = new LiquidJarShowVo();
        Page<LiquidJarDto> page = new Page<>(num, size);
        List<LiquidJarDto> mapList = liquidJarMapper.queryJarShowList(page, qryDto);
        lineVo.setPage(num);
        lineVo.setSize(size);
        lineVo.setTotal(page.getTotal());
        lineVo.setList(mapList);
        return lineVo;
    }

    @Override
    public LiquidJarShowVo queryJarShowIndex(LiquidJarDto qryDto, Integer num, Integer size) {
        LiquidJarShowVo lineVo = new LiquidJarShowVo();
        Page<LiquidJarDto> page = new Page<>(num, size);
        List<LiquidJarDto> mapList = liquidJarMapper.queryJarShowIndex(page, qryDto);
        lineVo.setPage(num);
        lineVo.setSize(size);
        lineVo.setTotal(page.getTotal());
        lineVo.setList(mapList);
        return lineVo;
    }

}
